package tech.summerly.weather.component.adapter

import android.support.v4.app.FragmentActivity
import me.drakeet.multitype.MultiTypeAdapter
import tech.summerly.weather.component.adapter.floors.*
import tech.summerly.weather.data.LiveStreamRepository
import tech.summerly.weather.data.bean.*
import tech.summerly.weather.model.Region

class WeatherTowerAdapter(val activity: FragmentActivity) : MultiTypeAdapter() {


    private var region: Region? = null

    init {
        register(RealTime::class.java, RealTimeFloor())
        register(Alerts::class.java, AlertFloor())
        register(DailyForecast::class.java, DailyForecastFloor())
        register(HourlyForecast::class.java, HourlyForecastFloor())
        register(Credit::class.java, CreditFloor())
        register(LifeStyle::class.java, LifeStyleFloor())
        register(BlockDivider::class.java, BlockDividerFloor())
        register(LiveStream::class.java, LiveStreamFloor())
        register(SunRiseSet::class.java, SunRiseSetFloor())
    }

    fun setWeather(realTime: RealTime,
                   forecast: Forecast,
                   region: Region) {
        this.region = region

        val items = ArrayList<Any>()
        items.add(realTime)
        forecast.alert?.let {
            items.add(Alerts(it))
        }
        items.add(forecast.daily)
        items.add(BlockDivider)

        items.add(forecast.hourly)
        items.add(BlockDivider)


        items.add(SunRiseSet(realTime, forecast.daily.astro))
        items.add(BlockDivider)

        if (LiveStreamRepository.isRegionSupport(region)) {
            items.add(LiveStream(region))
            items.add(BlockDivider)
        }

        items.add(LifeStyle(forecast.daily))
        items.add(Credit)
        this.items = items
        notifyDataSetChanged()
    }

    fun getRegion(): Region? = region


}

